Added PyGithub package to invoke github api calls#54
Conversation
colindean
left a comment
There was a problem hiding this comment.
So, this shaves… 5 lines of code, but we basically gain some standardization and gets pagination for free where we weren't using it (and may not actually need it…).
How do you feel about this? Is it worth the switch?
diff_poetry_lock/github.py
Outdated
| issue = self.repo.get_issue(int(self.s.pr_num)) | ||
| issue_comment = issue.get_comment(comment_id) | ||
| issue_comment.edit(f"{MAGIC_COMMENT_IDENTIFIER}{comment}") |
There was a problem hiding this comment.
thought: Hmmmmmmmmmmmm this is tripling the requests, eh?
I think we can use the data we already have to initialize an Issue and save the first and maybe also do it for IssueComment.
issue_comment = github.IssueComment(id=comment_id)but this might explicitly need a URL to be set, too. We have the info to build that URL…
issue_comment = github.IssueComment(
id=comment_id,
url=f"{self.s.api_url}/repos/{self.s.repository}/issues/comments/{comment_id}"
)could extract that URL building to a function…
def build_issue_comment_url(self, comment_id: int) -> str:
return f"{self.s.api_url}/repos/{self.s.repository}/issues/comments/{comment_id}"
issue_comment = github.IssueComment(
id=comment_id,
url=build_issue_comment_url(comment_id)
)This reduces the reliance on the library, which we're increasing drastically in this PR, but saves a few extra requests and thus a few tens of milliseconds of compute and API limits and whatnot. Probably worth it.
There was a problem hiding this comment.
Sure. Yes I'll look at customizing it
There was a problem hiding this comment.
Modified with a single api call
diff_poetry_lock/github.py
Outdated
| issue = self.repo.get_issue(int(self.s.pr_num)) | ||
| issue_comment = issue.get_comment(comment_id) | ||
| issue_comment.delete() |
There was a problem hiding this comment.
thought: Same deal, and likely same deal in other places where it takes a few more HTTP requests to get what we need when using the library… when we already have some or all of the metadata that the library is going to get in those requests.
There was a problem hiding this comment.
Replaced with package api helpers and a single api call
| class Headers(Enum): | ||
| """Enum for github api headers.""" | ||
|
|
||
| JSON = "application/vnd.github+json" | ||
| RAW = "application/vnd.github.raw" | ||
|
|
||
| def headers(self, token: str) -> dict[str, str]: | ||
| return {"Authorization": f"Bearer {token}", "Accept": self.value} |
There was a problem hiding this comment.
question: Is this still used? I think PyGitHub might provide this. Check out github/Auth.py, perhaps github.Token to wrap the token and github.Consts for the MIME types.
How we build the headers dict for the few requests we are managing… might still be this method.
There was a problem hiding this comment.
Yes the headers are used in the two get_file and resolve_commit_hashes that I didn't switch but yes I can take a look at Auth.py to reuse it
There was a problem hiding this comment.
The enums have been deleted now since the package api helpers handle it
I felt the library wasn't that worth to switch over to because I feel we have to customize each use case we've either to reduce the network calls or just that it doesn't support some use cases yet like the two calls we make: get_file and resolve_commit_hashes which still uses requests We could still incorporate it for standardization though since if we decide to go with the lib, we can set it up once after customization and reap its benefits |
Would it be worth suggesting those two methods to PyGitHub with a PR? |
For the pygithubs' get_contents -> fail?(because file size > 1MB) -> get_git_blob -> fail?(because file size > 100MB) -> get_contents (get the download_url) -> make requests call and stream to get the file As for the |
|
With my most recent changes, I've replaced all the github api calls with the package except get_file which I can take a look at in a separate pr to implement the following flow pygithubs' get_contents -> fail?(because file size > 1MB) -> get_git_blob -> fail?(because file size > 100MB) -> get_contents (get the download_url) -> make requests call and stream to get the file |
8da0c92 to
4338b50
Compare
1- Except get_file the rest use the package 2- get_file uses requests since iter_chunks helps with reading large files and using the package wasn't straightforward to handle large files Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
Replaced graphql api call with vanilla requests call since using the package doesn't have a value add Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
Currently, all changes are not being read by the action so fixing it by adding the ref with a depth of 0 to get all commits in the pr branch to checkout and run the tool Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
Fixed the action to checkout all changes to the poetry.lock file alone in pr and run the tool against the main branch Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
1- Remaining get_file call that still relies on requests might be addressed in a future pull request Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
f486b3e to
ce98977
Compare
Current setup is throwing a 403 when attempting to update an existing comment so added some token permissions to the action Signed-off-by: banginji <7316646+banginji@users.noreply.github.com>
|
After I merged in #56, it fixed the sha resolution in the diff pr comment but it is failing the 'diff-poetry-lock safe for forks' since it is a pull_request_target event which is looking at the base branch, main, but the env var for 'ref' has been changed to 'github_head_ref' instead of what it was previously 'github_ref' |
1- Except get_file and resolve_commit_hashes the rest use the package
2- get_file uses requests since iter_chunks helps with reading large files and using the package wasn't straightforward to handle large files
3- resolve_commit_hashes uses requests because the package didn't give a value add
This handles #47